﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//------------//
using QLKTX1._0.DTO;
using QLKTX1._0.DB;
using System.Data.SqlClient;
using System.Data;
//....................
namespace QLKTX1._0.DAO
{
    class QuyenDAO
    {
        public static List<QuyenDTO> dsQuyen()
        {
            List<QuyenDTO> dsQuyen = new List<QuyenDTO>();

            ConnectDB con = new ConnectDB();
            DataTable dt = con.getDataTable("SELECT * FROM Quyen");

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                QuyenDTO q = new QuyenDTO();

                q.MaQuyen = Int32.Parse (dt.Rows[i]["maQuyen"].ToString());
                q.TenQuyen = dt.Rows[i]["tenQuyen"].ToString();
                q.MoTaQ = dt.Rows[i]["moTaQ"].ToString();

                NhomQuyenDTO nq = new NhomQuyenDTO();
                nq.MaNhomQuyen = Int32.Parse(dt.Rows[i]["maNhomQuyen"].ToString());
                q.MaNhomQuyen = nq;

                dsQuyen.Add(q);           
              
            }

            return dsQuyen;
        }
        //Ham Insert
        public static bool themQuyen(QuyenDTO q)
        {
            ConnectDB con = new ConnectDB();// dung DB

            return con.executeQuery("INSERT INTO Quyen(tenQuyen,moTaQ,maNhomQuyen) VALUES('" + q.TenQuyen  + "','" + q.MoTaQ  + "'," + q.MaNhomQuyen .MaNhomQuyen  + ")");
        }
        public static bool xoaQuyen(QuyenDTO q)
        {
            ConnectDB con = new ConnectDB();
            return con.executeQuery("DELETE FROM Quyen WHERE maQuyen='" + q.MaQuyen  + "' ");
        }
        //ham cap nhat 
        public static bool capNhatQuyen(QuyenDTO q)
        {
            ConnectDB con = new ConnectDB();
            return con.executeQuery("UPDATE Quyen set tenQuyen='" + q.TenQuyen + "',moTaQ= '" + q.MoTaQ + "',maNhomQuyen= " + q.MaNhomQuyen.MaNhomQuyen + " WHERE maQuyen=" + q.MaQuyen + " ");
        }
    }
}
